package cn.zebra.dev.admin.module.brand.controller;

import cn.zebra.dev.admin.module.brand.dto.BrandDTO;
import cn.zebra.dev.admin.module.brand.dto.BrandNameListDTO;
import cn.zebra.dev.admin.module.brand.vo.BrandListVO;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.product.module.category.entity.CategoryEntity;
import cn.zebra.dev.product.module.category.service.CategoryService;
import cn.zebra.dev.product.moudle.brand.entity.BrandEntity;
import cn.zebra.dev.product.moudle.brand.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@Api(tags = "品牌")
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("品牌列表")
    public Result<List<BrandDTO>> list() {
        List<BrandEntity> brandEntities = brandService.findALL();
        List<Long> categoryIds = brandEntities.stream().map(BrandEntity::getBrandCategoryId).collect(Collectors.toList());
        List<CategoryEntity> categoryEntities = categoryService.findALLByIds(categoryIds);
        List<BrandDTO> brandDTOS = new LinkedList<>();
        if (brandEntities.size() > 0){
            brandEntities.forEach(brandEntity -> {
                BrandDTO brandDTO =new BrandDTO();
                BeanUtils.copyProperties(brandEntity , brandDTO);
                Optional<CategoryEntity> categoryEntityOptional = categoryEntities.stream()
                        .filter(categoryEntity -> categoryEntity.getId().equals(brandEntity.getBrandCategoryId())).findAny();
                if (categoryEntityOptional.isPresent()){
                    CategoryEntity categoryEntity = categoryEntityOptional.get();
                    brandDTO.setCategoryName(categoryEntity.getCategoryName());
                }
                brandDTOS.add(brandDTO);
            });
        }
        return Result.success(brandDTOS);
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取品牌信息")
    public Result<BrandDTO> get(@PathVariable Long id) {
        BrandEntity brandEntity = brandService.findById(id);
        if (brandEntity == null){
            return Result.error("不存在该品牌");
        }
        BrandDTO brandDTO =new BrandDTO();
        BeanUtils.copyProperties(brandEntity,brandDTO);
        return Result.success(brandDTO);
    }

    @PostMapping("/save")
    @ApiOperation("新增品牌信息")
    @Transactional(rollbackFor = Exception.class)
    public Result save(@Valid @RequestBody BrandListVO vo) {
        BrandEntity brandEntity = new BrandEntity();
        BeanUtils.copyProperties(vo, brandEntity);
        brandEntity.setId(null);
        brandEntity.setCreateTime(LocalDateTime.now());
        brandEntity.setDeleteFlag(false);
        brandEntity.setBrandCategoryId(vo.getCategoryId());
        if (!brandService.save(brandEntity)) {
            return Result.error("保存失败");
        }
        return Result.success("保存成功");
    }

    @GetMapping("/brandNameList")
    @ApiOperation("品牌名称列表")
    public Result<List<BrandNameListDTO>> brandNameList(){
        List<BrandEntity> brandEntity = brandService.findALL();
        List<BrandNameListDTO> brandNameListDTOS = new LinkedList<>();
        if (brandEntity.size() > 0){
            brandEntity.forEach(brandEntity1 -> {
                BrandNameListDTO brandNameListDTO = new BrandNameListDTO();
                BeanUtils.copyProperties(brandEntity1,brandNameListDTO);
                brandNameListDTOS.add(brandNameListDTO);
            });
        }
        return Result.success(brandNameListDTOS);
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改品牌信息")
    @Transactional(rollbackFor = Exception.class)
    public Result update(@PathVariable Long id, @Valid @RequestBody BrandListVO vo) {
        BrandEntity brandEntity = new BrandEntity();
        BeanUtils.copyProperties(vo, brandEntity);
        brandEntity.setId(null);
        brandEntity.setCreateTime(LocalDateTime.now());
        brandEntity.setDeleteFlag(false);
        brandEntity.setBrandCategoryId(vo.getCategoryId());
        if (!brandService.updateProductBrand(id, brandEntity)) {
            return Result.error("修改失败");
        }
        return Result.success("修改成功");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除品牌信息")
    public Result delete(@PathVariable Long id) {
        BrandEntity entity = new BrandEntity();
        entity.setDeleteFlag(true);
        if (brandService.update(entity, "id = {0}", id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败!");
    }

}
